package org.misty.seckill.util;

import org.misty.seckill.exception.AuthorizationException;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;

public class ArgsUtil {
  public static final Predicate<String> NOT_EMPTY_PREDICATE = s -> !StringUtils.isEmpty(s);

  public static Optional<String> notEmpty(String... args) {
    return Arrays.stream(args).filter(NOT_EMPTY_PREDICATE).findFirst();
  }

  public static <T> Optional<T> nonNull(T... args) {
    return Arrays.stream(args).filter(Objects::nonNull).findFirst();
  }

  public static void main(String[] args) {
//    System.out.println(notEmpty(AuthorizationException::throwSignOut, null, "", "string"));

//    System.out.println(notEmpty(AuthorizationException::throwSignOut, null, ""));

//    System.out.println(notEmpty("", null, "string").orElseThrow(AuthorizationException::signOut));

//    System.out.println(notEmpty("", null).orElseThrow(AuthorizationException::signOut));

    System.out.println(notEmpty("a123b456c789", null, "").orElseThrow(AuthorizationException::signOut));
  }
}
